/*******************************************************************************
																				
	DESCRIPTION:  	This do file produces figure 3.
					
*******************************************************************************/

clear all
global id_code 109

pause on
set seed 2110

/*******************************************************************************
*	Predicted job-finding rates - time series - predictions done by year
********************************************************************************/
		
foreach model in Full {

	* Import 2006 predictions:
	use "${data}/003_MainWithEnsemblePred_`model'_2006.dta", clear
	
	* Keep only sample who started spell in 2006:
	keep if !missing(p_emplAft6M_0M_In)		
	
	* Calculate average predicted JFRs at start of unemployment for samples
	* of individuals who reach each month of unemployment:
	cap frame drop dyn_sel
	frame create dyn_sel t obs double(f_pred f_emp)
	
	forval x = 0/12 {
		
		sum p_emplAft6M_0M_In if duration>=`x'*30
		local f_pred = r(mean)

		gen temp`x' = duration<`x'*30 + 6*30
		sum temp`x' if duration>=`x'*30
		local f_emp = r(mean)
		local obs = r(N)		
		
		frame post dyn_sel (`x') (`obs') (`f_pred') (`f_emp')
		
	}

	
	* Reshape and save
	frame change dyn_sel
		
		save "${output}/${id_code}_SelectionInJobFinding_`model'.dta", replace

		* Produce selection in job finding graph for full model:
		graph twoway (connected f_emp t, lcolor(orange_red) lwidth(medthick) mcolor(orange_red) msymbol(circle) msize(small)) ///
			(connected f_pred t, lcolor(ebblue) lwidth(medthick) mcolor(ebblue) msymbol(triangle) msize(small)) ///
			(pcarrowi `=f_emp[1]' 6 `=f_pred[7]' 6 (5) "`: di %3.2f `= 100 * (f_pred[7] - f_emp[1])'' p.p." ///
				`=f_pred[7]' 6 `=f_emp[7]' 6 (5) "`: di %3.2f `= 100 * (f_emp[7] - f_pred[7])'' p.p." ///
				`=f_emp[1]' 12 `=f_pred[13]' 12 (7) "`: di %3.2f `= 100 * (f_pred[13] - f_emp[1])'' p.p." ///
				`=f_pred[13]' 12 `=f_emp[13]' 12 (7) "`: di %3.2f `= 100 * (f_emp[13] - f_pred[13])'' p.p.", ///
				mcolor(gray) mlabcolor(gs5) mlabsize(small) lcolor(gray)), ///
			yline(`=f_emp[1]', lcolor(gray) lpattern(dash)) ylabel(, angle(0)) ///
			xtitle("Time (months)") xlabel(0(3)12) xtick(0(1)12) ///
			ytitle("") ylabel(0.2(0.1)1) ///
			legend(order(1 "Empirical Job-Finding Probability d Months into Spell" 2 "Pred. Job-Finding Probability at Start of Spell, for spells lasting d months") rows(2) size(small)) ///
			graphregion(color(white)) ///
			name(sel`model', replace)
		graph export "${output}/${id_code}_SelectionInJobFinding_`model'.pdf", as(pdf) replace 

		di "Dynamic selection accounts for `: di %3.2f `= 100 * (f_pred[7] - f_emp[1])/ (f_emp[7] - f_emp[1])''% of the decline in JFR from month 0 to month 6."
		di "Dynamic selection accounts for `: di %3.2f `= 100 * (f_pred[13] - f_emp[1])/ (f_emp[13] - f_emp[1])''% of the decline in JFR from month 0 to month 12."
	
	frame change default
	
}